package com.service.impl;

import com.common.exception.db.InsertException;
import com.dao.BillDao;
import com.dao.RewardDao;
import com.entity.RewardInfo;
import com.service.RewardService;
import com.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2019/1/10 0010.
 */
@Service
public class RewardServiceImpl implements RewardService {
    @Autowired
    private RewardDao dao;
    @Autowired
    private BillDao billDao;


    @Override
    public List<RewardInfo> findRewardList(Integer limit) {
        return dao.findRewardList(limit);
    }

    @Override
    public int updatePrice(Integer userId, String hourPrice) {
        return dao.updatePrice(userId, hourPrice);
    }

    @Override
    @Transactional
    public int addMoney(Integer userId, BigDecimal money, String nonceStr,BigDecimal discount) {
        int count = dao.addMoney(userId, money, nonceStr,discount);
        if (count > 0) {
            String uuid = UUIDUtil.get32UUID();
            count = billDao.insertBill(userId, money, 1, "充值金额", uuid);
            if (count > 0) {
                count = dao.updateMoney(userId, money);
                if (count > 0) {
                    return count;
                } else {
                    throw new InsertException("编辑失败");
                }
            } else {
                throw new InsertException("新增失败");
            }
        } else
            throw new InsertException("新增失败");
    }
}
